<template>
  <div class="warp">
    <div class="ward">二十一病区(日间治疗中心)</div>
    <div class="main">
      <div class="tltle">{{ title }}</div>
      <div class="table-box">
        <el-table
          :data="tableData"
          :span-method="objectSpanMethod"
          border
          height="100%"
          style="width: 100%;"
          :cell-style="cellStyle"
        >
          <el-table-column prop="week" label="日期" width="250" align="center">
          </el-table-column>
          <el-table-column
            prop="timeSlot"
            label="班次"
            width="300"
            align="center"
          >
          </el-table-column>
          <el-table-column label="已预约/总数" width="250" align="center">
            <template v-slot="{ row }"
              ><span>{{ row.currCount }}/{{ row.total }}</span></template
            >
          </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template v-slot="{ row }">
              <el-button
                type="primary"
                round
                v-if="!impatientInfo.appointmentTime && row.currCount < row.total"
                @click="handleTableAppoint(row)"
                >预约</el-button
              >
              <el-button
                type="danger"
                round
                v-if="
                  row.szrq + ' ' + row.timeSlot ==
                    impatientInfo.appointmentTime +
                      ' ' +
                      impatientInfo.appointmentClasses
                "
                @click="handleCancelAppoint()"
                >取消预约</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="right-warp">
      <div class="card-box">
        <div class="card-title">
          <span>患者信息</span>
        </div>
        <div class="card-content">
          <div class="text item">
            <span>患者:</span>{{ impatientInfo.name }}
          </div>
          <div class="text item">
            <span>科室:</span>{{ impatientInfo.deptName }}
          </div>
          <div class="text item" v-show="impatientInfo.appointmentTime">
            <span>预约时间:</span>{{ impatientInfo.appointmentTime }}
            {{ impatientInfo.appointmentClasses }}
          </div>
          <div class="text item">
            <span>入院诊断:</span>
            <div class="txt-content">
              {{ impatientInfo.diagName }}
            </div>
          </div>
          <div class="text item">
            <span style="padding-top: 5px;">状态:</span>
            <el-row v-if="impatientInfo.appointmentTime != null">
              <el-tag>已预约</el-tag>
              <el-button type="danger" round size="small" @click="handleCancelAppoint()">取消预约</el-button>
            </el-row>
            <el-row v-else>
              <el-tag type="danger">未预约</el-tag>
            </el-row>
          </div>
          <div class="text item"></div>
        </div>
      </div>
      <div class="card-box">
        <div class="card-title">住院信息</div>
        <div class="card-content"></div>
      </div>
    </div>
  </div>
</template>
<script>
// import {
//   getIiPrepayinByIdRJ,
//   searchRJSS,
//   searchRJSZ,
//   addPatientBed,
//   delPatientBed,
// } from "@/utils/apis/public";

import {
  getIiPrepayinByIdRJ,
  searchRJSS,
  searchRJSZ,
  addPatientBed,
  delPatientBed,
} from "@/utils/apis/rjbf";
export default {
  data() {
    return {
      tableLoading: false,
      title: "",
      iindType: 1,
      tableData: [],
      urlParam: {},
      impatientId: "",
      impatientInfo: {},
      timeSlotNum: 1,
    };
  },
  created() {
    this.urlParam = this.$route.query;
    this.getImpatientInfo();
  },
  methods: {
    cellStyle() {
      return {
        height: "70px",
      };
    },
    //获取病人信息
    getImpatientInfo() {
      let param = {
        preid: this.urlParam?.preid,
      };
      //根据返回信息的标识调用
      getIiPrepayinByIdRJ(param).then((res) => {
        if (res.data.code === 200  && res.data.data!=null) {
          this.impatientInfo = res.data.data;
          this.impatientId = res.data.data?.id;
          this.iindType = res.data.data?.iinpType || "1";
          if (this.iindType === "1" || this.iindType === "2") {
            // 日间手术
            this.getDJTableData(searchRJSS);
          } else if (this.iindType === "4") {
            // 日间输注
            this.getDJTableData(searchRJSZ);
          }
        }
        else{
              this.$message.error("该患者不属于日间患者,无法预约,请确认是否已经在患者便签栏中选择“日间手术”或“日间输注”");
            }
      });
    },
    //调用预约列表接口
    getDJTableData(api) {
      this.tableLoading = true;
      api().then((res) => {
        this.tableLoading = false;
        if (res.data.code === 200) {
          this.timeSlotNum = res.data.data.timeSlotNum;
          this.tableData = res.data.data.rjszVOS;
        }
      });
    },

    // 预约
    handleTableAppoint(row) {
      const param = {
        docCode: this.urlParam?.jobnumber,
        docName: this.urlParam?.createBy,
        jobnumber: this.urlParam?.jobnumber,
        createBy: this.urlParam?.createBy,
        bedType: this.iindType,
        appoinmentTime: row.szrq,
        appointmentClasses: row.timeSlot,
        preid: this.urlParam?.preid,
      };
      addPatientBed(param).then((res) => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message);
          this.getImpatientInfo();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    //取消预约
    handleCancelAppoint() {
      console.info('cww',this.impatientId)
      if (!this.impatientId) {
        this.$message.warning("当前无病人信息,不能取消预约操作");
        return;
      }
      this.$confirm({
        content: "确定要取消预约吗？",
        title: "确认信息",
        okFn: (res) => {
          delPatientBed({
            id: this.impatientId,
            operName:`${this.$route.query.createBy}`&&`${this.$route.query.createBy}${this.$route.query.jobnumber}`||''
          }).then((res) => {
            if (res.data.code === 200) {
              this.$message.success(res.data.message);
              this.getImpatientInfo();
            } else {
              this.$message.error(res.data.message);
            }
          });
        },
        cancelFn: (res) => {
          console.log("取消");
        },
      });
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (rowIndex % this.timeSlotNum === 0) {
          return {
            rowspan: this.timeSlotNum,
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },
  },
  watch: {
    iindType: {
      immediate: true,
      handler(val) {
        this.title = val === "4" ? "日间输注" : "日间手术";
      },
    },
  },
};
</script>
<style lang="less" scoped>
.warp {
  height: 99vh;
  width: 99vw;
  background-color: #e9e7e7;
  display: flex;
  text-align: center;
  overflow: hidden;
}
.ward {
  flex: 0 0 40px;
  width: 40px;
  background-color: #2b80ff;
  font-size: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  -webkit-writing-mode: vertical-rl;
  writing-mode: vertical-rl;
  margin: 5px 0px;
}
.main {
  flex: 1;
  background-color: white;
  /* margin-right: 10px; */
  overflow: hidden;
  margin: 5px 10px 5px 5px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.tltle {
  height: 30px;
  padding: 10px;
  border-bottom-style: solid;
  border-bottom-width: 1px;
  border-bottom-color: #e9e7e7;
  background-color: white;
  font-size: 20px;
}
.table-box {
  padding: 50px;
  display: flex;
  height: 100%;
  flex: 1;
  overflow: hidden;
}

.text {
  font-size: 14px;
}

.item {
  padding: 5px;
  text-align: left;
  display: flex;
  span {
    display: inline-block;
    width: 65px;
    text-align: right;
    margin-right: 10px;
    font-weight: 600;
  }
  .txt-content {
    flex: 1;
  }
}
.right-warp {
  flex: 0 0 280px;
  width: 280px;
  // color: #555555;
  background-color: white;
  // border: 1px solid red;
  margin: 5px 5px 5px 5px;
  .card-box {
    &:nth-child(2) {
      margin-top: 10px;
    }
    .card-title {
      height: 35px;
      // border: 1px solid orange;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0px 10px;
      background-color: #f4f8fb;
    }
  }
}
</style>
